{% extends 'base.html' %}
{% block content %}
<div class="container mt-4">
  <h2>发新帖</h2>
  <form method="post" enctype="multipart/form-data">
    <div class="mb-3">
      <label for="title" class="form-label">标题</label>
      <input type="text" class="form-control" id="title" name="title" required>
    </div>
    <div class="mb-3">
      <label for="content" class="form-label">内容</label>
      <textarea class="form-control" id="content" name="content" rows="6" required></textarea>
    </div>
    <div class="mb-3">
      <label for="file" class="form-label">附件</label>
      <input type="file" class="form-control" id="file" name="file" accept=".pdf,.doc,.docx,.jpg,.jpeg,.png,.gif,.txt">
      <div class="form-text">支持的文件类型: PDF, Word, 图片, 文本文件</div>
    </div>
    <div class="mb-3">
      <label for="preview" class="form-label">预览</label>
      <div id="preview" class="border p-3"></div>
    </div>
    <button type="submit" class="btn btn-success">发布</button>
    <a href="{{ url_for('board') }}" class="btn btn-secondary">返回</a>
    <a href="https://www.runoob.com/html/html-tutorial.html" class="btn btn-info" target="_blank">查看Markdown使用文档</a>
  </form>
</div>

<!-- 引入Showdown.js -->
<script src="https://cdn.jsdelivr.net/npm/showdown/dist/showdown.min.js"></script>
<script>
  document.addEventListener('DOMContentLoaded', function () {
    const converter = new showdown.Converter();
    const contentTextarea = document.getElementById('content');
    const previewDiv = document.getElementById('preview');
    const fileInput = document.getElementById('file');

    // 实时预览Markdown内容
    contentTextarea.addEventListener('input', function () {
      const markdownText = contentTextarea.value;
      const htmlText = converter.makeHtml(markdownText);
      previewDiv.innerHTML = htmlText;
    });

    // 文件上传处理（不读取内容）
    fileInput.addEventListener('change', function (event) {
      const file = event.target.files[0];
      if (file) {
        // 仅验证文件类型，不读取内容
        const validTypes = ['application/pdf', 'application/msword', 
                          'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                          'image/jpeg', 'image/png', 'image/gif', 'text/plain'];
        if (!validTypes.includes(file.type)) {
          alert('不支持的文件类型');
          fileInput.value = '';
        }
      }
    });
  });
</script>
{% endblock %}